﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Threading;
using System.Globalization;


namespace University.Workshop.Business
{
static public class MedicalSuppliesManager
    {


        public static void RegisterNewMedicalSupplies(string itemName, string itemId, string lot, DateTime date, string quantity)
        {
           

            if (string.IsNullOrEmpty(itemName))
                throw new InvalidOperationException("All fields are required.");
            if (string.IsNullOrEmpty(itemId))
                throw new InvalidOperationException("All fields are required.");
            if (string.IsNullOrEmpty(lot))
                throw new InvalidOperationException("All fields are required.");
            if (string.IsNullOrEmpty(quantity))
                throw new InvalidOperationException("All fields are required.");
            int a = DateTime.Now.CompareTo(date);
            if (a > 0)
                throw new InvalidOperationException("Date Cannot be older than today");
            int quantityNumber = -1;
            try
            {
                quantityNumber = int.Parse(quantity, CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                throw new InvalidOperationException("Invalid Quantity");
            }
            if (quantityNumber < 1)
                throw new InvalidOperationException("Quantity cannot be less than 1");
            MedicalSupplies supplie = new MedicalSupplies(itemName, itemId, lot, date.ToString(), quantityNumber);
            if (!MedicalSuppliesDataAccess.RegisterNewMedicalSupplies(supplie))
                throw new InvalidOperationException("The Id already exists");

        }



        public static ICollection<MedicalSupplies> GetMedicalSuppliesForName(string name)
        {
            return MedicalSuppliesDataAccess.GetMedicalSuppliesForName(name);
        }

        public static void ModifyQuantity(MedicalSupplies item)
        {
            //      if (Quantity == string.Empty)
            //          throw new Exception("debe llenar el campo a actualizar");
            //      else
            //      {
            //          int j;
            //         j=int.Parse(Quantity);

            MedicalSuppliesDataAccess.ModifyQuantity(item);
        }

    }
}
